<?xml version="1.0" encoding="UTF-8"?>
<project name="waffle" default="build" basedir=".">
	<property file="${basedir}/../Version/Version.properties" />
	<property name="waffle.version" value="${product.version}" />
	<property name="waffle.java.version" value="1.5" />
	<property name="waffle.copyright" value="${copyright.text}" />
	<property name="waffle.root" value="${basedir}" />
	<property name="waffle.build" value="${waffle.root}/build" />
	<property name="waffle.doc" value="${waffle.root}/doc" />
	<property name="waffle.lib" value="${waffle.root}/lib" />
	<property name="waffle.src" value="${waffle.root}/src" />
	<property name="waffle.cover" value="${waffle.root}/cover" />
	<property name="waffle.test.src" value="${waffle.root}/src-test" />
	<property name="waffle.test.lib" value="${waffle.root}/lib-test" />
	<property name="waffle.test" value="${waffle.root}/test" />
	<property name="thirdparty.dir" location="${basedir}/../ThirdParty" />
	<!-- compile -->
	<path id="jna.classpath">
		<fileset dir="${thirdparty.dir}/jna">
			<include name="jna.jar" />
			<include name="platform.jar" />
		</fileset>
	</path>
	<path id="tomcat.classpath">
		<fileset dir="${thirdparty.dir}/tomcat">
			<include name="**/*.jar" />
		</fileset>
	</path>
	<path id="commons-logging.classpath">
		<fileset dir="${thirdparty.dir}/commons-logging">
			<include name="**/*.jar" />
		</fileset>
	</path>
	<path id="spring.classpath">
		<fileset dir="${thirdparty.dir}/spring/dist">
			<include name="**/*.jar" />
		</fileset>
		<fileset dir="${thirdparty.dir}/spring-security/dist">
			<include name="**/*.jar" />
		</fileset>
	</path>
	<path id="guava.classpath">
		<fileset dir="${thirdparty.dir}/guava">
			<include name="**/*.jar" />
		</fileset>
	</path>
	<path id="waffle.classpath">
		<path refid="jna.classpath" />
		<path refid="tomcat.classpath" />
		<path refid="commons-logging.classpath" />
		<path refid="spring.classpath" />
		<path refid="guava.classpath" />
	</path>
	<macrodef name="compile">
		<attribute name="optimize" />
		<attribute name="debug" />
		<sequential>
			<delete dir="${waffle.lib}" />
			<mkdir dir="${waffle.lib}" />
			<javac target="${waffle.java.version}" srcdir="${waffle.src}"
				destdir="${waffle.lib}" optimize="@{optimize}" debug="@{debug}" 
				includeantruntime="false">
				<classpath refid="waffle.classpath" />
				<include name="**/*.java" />
			</javac>
		</sequential>
	</macrodef>
	<!-- test -->
	<path id="groboutils.classpath">
		<fileset dir="${thirdparty.dir}/groboutils/lib">
			<include name="GroboUtils-5-core.jar" />
		</fileset>
	</path>
	<path id="waffle.test.classpath">
		<pathelement location="${waffle.lib}" />
		<path refid="waffle.classpath" />
		<fileset dir="${thirdparty.dir}/junit">
			<include name="*.jar" />
		</fileset>
		<pathelement location="${waffle.test.src}" />
		<pathelement location="${waffle.test.lib}" />
		<path refid="groboutils.classpath" />
		<path refid="emma.classpath" />
	</path>
	<target name="compile-tests">
		<mkdir dir="${waffle.test.lib}" />
		<javac target="${waffle.java.version}" srcdir="${waffle.test.src}"
			destdir="${waffle.test.lib}" optimize="no" debug="yes" includeantruntime="false">
			<classpath refid="waffle.test.classpath" />
			<include name="**/*.java" />
		</javac>
	</target>
	<target name="test-report">
		<mkdir dir="${waffle.test}/report" />
		<junitreport todir="${waffle.test}/report">
			<fileset dir="${waffle.test}" includes="TEST-*.xml" />
			<report todir="${waffle.test}/report" />
		</junitreport>
		<echo message="See ${waffle.test}\report\index.html" />
	</target>
	<target name="test" depends="compile-tests">
		<mkdir dir="${waffle.test}" />
		<junit fork="true" newenvironment="true" failureproperty="tests.failed"
			haltonfailure="no" haltonerror="no" printsummary="on" errorproperty="tests.failed">
			<batchtest todir="${waffle.test}">
				<fileset dir="${waffle.test.lib}" includes="**/*Tests.class"
					excludes="**/*LoadTests.class" />
			</batchtest>
			<formatter type="xml" />
			<classpath refid="waffle.test.classpath" />
			<jvmarg value="-Demma.coverage.out.file=${waffle.cover}/coverage.emma" />
		</junit>
		<antcall target="test-report" />
		<fail message="Unit Tests failed" if="tests.failed" />
	</target>
	<!-- cover -->
	<property name="emma.dir" value="${thirdparty.dir}/emma/lib" />
	<path id="emma.classpath">
		<fileset dir="${thirdparty.dir}/emma/lib">
			<include name="emma.jar" />
			<include name="emma_ant.jar" />
		</fileset>
	</path>
	<taskdef resource="emma_ant.properties" classpathref="emma.classpath" />
	<target name="instrument">
		<echo message="Instrumenting ${waffle.lib}" />
		<path id="build.classpath">
			<pathelement path="${waffle.lib}" />
		</path>
		<emma>
			<instr instrpathref="build.classpath" destdir="${waffle.cover}/lib"
				metadatafile="${waffle.cover}/metadata.emma" merge="true" />
		</emma>
		<copy todir="${waffle.lib}">
			<fileset dir="${waffle.cover}/lib" includes="**/*" />
		</copy>
	</target>
	<target name="cover-report">
		<emma>
			<report sourcepath="${waffle.src}">
				<fileset dir="${waffle.cover}">
					<include name="*.emma" />
				</fileset>
				<html outfile="${waffle.cover}/coverage.html" />			
			</report>
		</emma>
	</target>
	<target name="cover">
		<compile debug="yes" optimize="no" />
		<antcall target="instrument" />
		<antcall target="test" />
		<antcall target="cover-report" />
	</target>
	<!-- package -->
	<target name="jar">
		<delete dir="${waffle.build}" />
		<mkdir dir="${waffle.build}" />
		<jar jarfile="${waffle.build}/waffle-jna.jar">
			<fileset dir="${waffle.lib}">
				<include name="waffle/**/*.class" />
			</fileset>
			<manifest>
				<attribute name="Version" value="${waffle.version}" />
				<attribute name="Created-By" value="${waffle.copyright}" />
			</manifest>		
		</jar>
		<jar jarfile="${waffle.build}/waffle-src.zip">
			<fileset dir="${waffle.src}">
				<include name="waffle/**/*.java" />
			</fileset>
			<manifest>
				<attribute name="Version" value="${waffle.version}" />
				<attribute name="Created-By" value="${waffle.copyright}" />
			</manifest>
		</jar>
	</target>
	<target name="doc">
		<javadoc destdir="${waffle.doc}" sourcepath="${waffle.src}"
			classpathref="waffle.classpath" />
	</target>
	<target name="clean">
		<delete dir="${waffle.build}" />
		<delete dir="${waffle.lib}" />
		<delete dir="${waffle.test.lib}" />
		<delete dir="${waffle.doc}" />
		<delete dir="${waffle.cover}" />
		<delete dir="${waffle.test}" />
	</target>
	<target name="compile">
		<echo message="Building version ${waffle.version}" />
		<compile debug="no" optimize="yes" />
		<antcall target="jar" />
	</target>
	<target name="build">
		<echo message="Building version ${waffle.version}" />
		<compile debug="no" optimize="yes" />
		<antcall target="jar" />
		<antcall target="test" />
		<antcall target="doc" />
	</target>
	<target name="clean-build" depends="clean,build" />
</project>
